<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>doh.robot Button Test</title>

		<style>
			@import "../../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../../dojo/dojo.js"
			djConfig="isDebug: true, parseOnLoad: true"></script>

		<script type="text/javascript" src="../../helpers.js"></script>

		<script type="text/javascript">
			dojo.require("dijit.robotx");

			dojo.addOnLoad(function(){
				doh.robot.initRobot('../test_Button.html');

				var submitCount = 0, resetCount = 0;


				doh.register("setup", function(){
					doh.robot.sequence(function(){
						dojo.connect(dijit.byId("testForm"), "onSubmit", function(){
							submitCount++;
						});
						dojo.connect(dijit.byId("testForm"), "onReset", function(){
							resetCount++;
						})
					});
				});

				doh.register("dijit.form.Button", [
					{
						name: "enabled",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){ dojo.byId("input").focus(); });

							var clicks = 0;
							dijit.byId("1465").set("onClick", function(){ clicks++; });


							// tab into button to get focus
							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							// and activate	with space / enter
							doh.robot.keyPress(dojo.keys.SPACE, 500, {});
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(2, clicks, "both space and enter click the button");
							}), 500);
							return d;
						}
					},
					{
						name: "disabled no focus",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.is("1465", dijit.getEnclosingWidget(dojo.global.dijit._curFocus).id, "focus starting on 1465");

							dijit.byId("1467").set("disabled", true);

							// tab over disabled "View" button to "Create" combo-button
							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								console.log("current focus is ", dojo.global.dijit._curFocus);
								doh.is("comboCreate", dijit.getEnclosingWidget(dojo.global.dijit._curFocus).id, "focus jumped to combo create (left part of button)");
							}), 500);
							return d;
						}
					}
				]);

				doh.register("dijit.form.DropDownButton", [
					{
						name: "down arrow opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("cut", dojo.global.dijit._curFocus.id, "focus is on menu");
							}), 500);
							return d;
						}
					},

					{
						name: "esc closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.keyPress(dojo.keys.ESCAPE, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					},
					{
						name: "space opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.SPACE, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},

					{
						name: "space executes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.keyPress(dojo.keys.SPACE, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					},
					{
						name: "enter opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},

					{
						name: "tab key closes popup, goes back to button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					},
					{
						name: "enter opens menu again",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							dijit.byId("edit").focus();
							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("editMenu"), "edit menu is visible: " + dijit.byId("editMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},

					{
						name: "enter closes menu (#4772)",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("editMenu"), "edit menu is hidden: " + dijit.byId("editMenu").domNode.style.cssText);
								doh.is("edit", dojo.global.dijit._curFocus.id, "focus is back on button");
							}), 1000);
							return d;
						}
					}

				]);

				doh.register("dijit.form.ComboButton", [
					{
						name: "tab to button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){
								dijit.byId("color").focus();
							});

							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							var leftPart = dojo.query(".dijitButtonContents", dojo.byId("save"))[0];
							doh.t(leftPart, "found left part of button");

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(leftPart, dojo.global.dijit._curFocus, "focused on left part");
								console.log("cur focus: ",  dojo.global.dijit._curFocus);
							}), 1000);
							return d;
						}
					},

					{
						name: "click button",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var clicked = false;
							dijit.byId("save").set("onClick", function(){ clicked = true; });

							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(clicked, "button was clicked");
							}), 1000);
							return d;
						}
					},

					{
						name: "tab to drop down arrow",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(rightPart, dojo.global.dijit._curFocus, "focused on right part of combo");
							}), 1000);
							return d;
						}
					},

					{
						name: "down arrow opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("saveMenu"), "save menu is visible: " + dijit.byId("saveMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},

					{
						name: "esc closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.ESCAPE, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("saveMenu"), "save menu was closed: " + dijit.byId("saveMenu").domNode.style.cssText);
								doh.is(rightPart, dojo.global.dijit._curFocus, "refocused on right part of combo");
							}), 1000);
							return d;
						}
					},

					{
						name: "enter key opens menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.keyPress(dojo.keys.ENTER, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isVisible("saveMenu"), "save menu is visible: " + dijit.byId("saveMenu").domNode.style.cssText);
							}), 1000);
							return d;
						}
					},

					{
						name: "tab closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var rightPart = dojo.query(".dijitDownArrowButton", dojo.byId("save"))[0];
							doh.t(rightPart, "found right part of button");

							doh.robot.keyPress(dojo.keys.TAB, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.t(isHidden("saveMenu"), "save menu was closed: " + dijit.byId("saveMenu").domNode.style.cssText);
								doh.is(rightPart, dojo.global.dijit._curFocus, "refocused on right part of combo");
							}), 1000);
							return d;
						}
					},

					{
						name: "tab closes menu",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(function(){
								// Focus element before "save" ComboButton
								dijit.byId("color").focus();

								// And disable the ComboButton
								dijit.byId("save").set("disabled", true);
							});

							// Tab should skip over the ComboButton entirely
							doh.robot.keyPress(dojo.keys.TAB, 100, {});

							doh.robot.sequence(d.getTestCallback(function(){
								var focusedWidget = dijit.getEnclosingWidget(dojo.global.dijit._curFocus);
								doh.isNot("save", focusedWidget && focusedWidget.id, "didn't focus on combo");
							}), 500);
							return d;
						}
					}
				]);

				doh.register("dijit.form.ToggleButton", [
					{
						name: "change value",
						timeout: 15000,
						runTest: function(){
							var d = new doh.Deferred();

							var checked = dijit.byId("toggle1").get("checked");
							dijit.byId("toggle1").set("onChange", function(v){ checked = v; });
							doh.t(checked, "toggle1 initially checked");

							doh.robot.sequence(function(){
								dijit.byId("toggle1").focus();
							});
							doh.robot.keyPress(dojo.keys.SPACE, 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.f(checked, "toggle1 unchecked");
							}), 1000);
							return d;
						}
					}
				]);

				dojo.forEach(["SPACE", "ENTER"], function(key){
					doh.register("Invoke Submit and Reset Buttons by " + key, [
					{
						name: "click Submit",
						timeout: 5000,
						runTest: function(){
							var d = new doh.Deferred();
							submitCount = 0;
							doh.robot.sequence(function(){
								dijit.byId("bSubmit").focus();
							});
							doh.robot.keyPress(dojo.keys[key], 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(1, submitCount, (key + ": # of times submit invoked " + submitCount));
							}), 500);

							return d;
						}
					},

					{
						name: "click Reset",
						timeout: 7000,
						runTest: function(){
							var d = new doh.Deferred();
							resetCount = 0;
							dijit.byId("testName").value = "";


							doh.robot.sequence(function(){
								dijit.byId("testName").focus();
							},500);
							doh.robot.typeKeys("test", 500, 2000);

							doh.robot.sequence(function(){
								dijit.byId("bReset").focus();
							},500);
							doh.robot.keyPress(dojo.keys[key], 500, {});

							doh.robot.sequence(d.getTestCallback(function(){
								doh.is(1, resetCount, (key + ": reset should be invoked 1 time. actual count = " + resetCount));
								doh.is("",dijit.byId("testName").value, "Name field has been reset");
							}), 1000);
							return d;
						}
					}
				]);
				});

				doh.register("Submit via ENTER key on input", {
					name: "enter on input",
					timeout: 5000,
					runTest: function(){
						var d = new doh.Deferred();
						submitCount = 0;
						doh.robot.sequence(function(){
							dijit.byId("testName").focus();
						});
						doh.robot.keyPress(dojo.keys.ENTER, 500, {});

						doh.robot.sequence(d.getTestCallback(function(){
							doh.is(1, submitCount, "submitted via enter key on input");
						}), 500);

						return d;
					}
				});

				doh.run();
			});
		</script>
	</head>
</html>
